package com.jason.kotlin.meta.architecture2.ui

import android.os.Bundle
import android.text.InputType
import android.util.Log
import android.view.View
import android.widget.CheckBox
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.ViewModelProvider
import com.jason.kotlin.meta.R
import com.jason.kotlin.meta.architecture2.viewmodel.UserViewModel
import com.jason.library.common.base.Status
import com.jason.library.common.ext.afterTextChange
import com.jason.library.common.ext.observe
import com.jason.library.common.ext.showToast
import com.jason.library.common.ext.start

/**
 * 玩Android 开放API的登录
 */
class LoginActivity : AppCompatActivity() {

    private lateinit var toolbar: Toolbar

    private lateinit var usernameEdit: EditText
    private lateinit var clearIv: ImageView
    private lateinit var passwordEdit: EditText
    private lateinit var visibleCb: CheckBox
    private lateinit var loginBtn: TextView
    private lateinit var registerTv: TextView


    private lateinit var viewModel: UserViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        initViewModel()
        initView()
        initListener()
    }

    private fun initView() {
        toolbar = findViewById(R.id.toolbar)
        usernameEdit = findViewById(R.id.login_username)
        clearIv = findViewById(R.id.login_clear)
        passwordEdit = findViewById(R.id.login_pwd)
        visibleCb = findViewById(R.id.login_key)
        loginBtn = findViewById(R.id.login_sub)
        registerTv = findViewById(R.id.login_go_register)
    }

    private fun initListener() {
        toolbar.run {
            setSupportActionBar(this)
            title = "登录"
            setNavigationIcon(R.drawable.ic_close)
            setNavigationOnClickListener { finish() }
        }

        usernameEdit.afterTextChange {
            if (it.isNotEmpty()) {
                clearIv.visibility = View.VISIBLE
            } else {
                clearIv.visibility = View.GONE
            }
        }
        passwordEdit.afterTextChange {
            if (it.isNotEmpty()) {
                visibleCb.visibility = View.VISIBLE
            } else {
                visibleCb.visibility = View.GONE
            }
        }

        clearIv.setOnClickListener {
            usernameEdit.setText("")
        }

        loginBtn.setOnClickListener {
            when {
                usernameEdit.text.isEmpty() -> usernameEdit.error = "请填写账号"
                passwordEdit.text.isEmpty() -> passwordEdit.error = "请填写密码"
                else -> viewModel.login(usernameEdit.text.toString(), passwordEdit.text.toString())
            }
        }

        registerTv.setOnClickListener {
            start(RegisterActivity::class.java)
        }


        visibleCb.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                passwordEdit.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
            } else {
                passwordEdit.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
            }
            passwordEdit.setSelection(passwordEdit.text.toString().length)
        }
    }




    private fun initViewModel() {
        viewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(application).create(
            UserViewModel::class.java
        )
        bindLiveData()
    }


    /**
     * 绑定LiveData的监听
     */
    private fun bindLiveData() {

        observe(viewModel.pageStatusData) {
            when (it.status) {
                Status.SUCCESS -> {

                }
                Status.EMPTY -> showToast("加载不到数据")
                Status.ERROR -> showToast("加载失败")
                Status.LOADING -> {
                }
            }
        }

        //banner 数据监听
        observe(viewModel.userData) {
            when(it.status){
                Status.SUCCESS -> {
                    val user = it.data
                    Log.i("chenby","user = $user.toString()")
                    showToast("登录成功：$user.toString()")
                    finish()
                }
                Status.ERROR -> {
                    showToast("登录成功：${it.errorMsg}")
                }
            }
        }
    }
}